﻿using System;
using System.Collections.Generic;
using PagedList;

namespace CodeCamp.Models.Logs
{
    public interface ILogSearchFiler
    {
        /// <summary>
        /// Time filter was set up
        /// </summary>
        Nullable<DateTime> FilterTime { get; }

        /// <summary>
        /// Filter value RemoteIP
        /// </summary>
        string RemoteIP { get; }
        bool RemoteIPExcludeNull { get; }
        bool RemoteIPNotEq { get; }

        /// <summary>
        /// Filter value UserName
        /// </summary>
        string UserName { get; }
        bool UserNameExcludeNull { get; }
        bool UserNameNotEq { get; }

        List<LogLevelViewModel> LogLevels { get; }
        bool LogLevelsNot { get; }
    }

    public class LogSearchViewModel : ILogSearchFiler
    {
        public LogSearchViewModel()
        {
            LogLevels = new List<LogLevelViewModel>();
        }

        public string ClearButton { get; set; }
        public string EntryTimeZone { get; set; }
        public Nullable<int> HeadlineMaxLen { get; set; }
        public Nullable<int> Page { get; set; }
        public IPagedList<LogListEntryViewModel> SearchResults { get; set; }
        public string SearchButton { get; set; }
        public string RemoteIP { get; set; }
        public bool RemoteIPExcludeNull { get; set; }
        public bool RemoteIPNotEq { get; set; }
        public Nullable<DateTime> FilterTime { get; set; }
        public Nullable<long> FilterTimeTicks
        {
            get
            {
                long? noTicks = null;
                return FilterTime.HasValue ? FilterTime.Value.Ticks : noTicks;
            }
            set
            {
                if (value.HasValue)
                    FilterTime = new DateTime(value.Value);
                else
                    FilterTime = null;
            }
        }
        public string UserName { get; set; }
        public bool UserNameExcludeNull { get; set; }
        public bool UserNameNotEq { get; set; }
        public List<LogLevelViewModel> LogLevels { get; set; }
        public bool LogLevelsNot { get; set; }
        public string UTCOffsetMinutes { get; set; }
        public Nullable<int> UTCAdjustMinutes
        {
            get
            {
                Nullable<int> utcAdjustMinutes = null;
                int utcAdjMin;
                if (!string.IsNullOrWhiteSpace(UTCOffsetMinutes) && Int32.TryParse(UTCOffsetMinutes, out utcAdjMin))
                    utcAdjustMinutes = utcAdjMin * -1;
                return utcAdjustMinutes;
            }
        }
    }
}